/*
* Suake2.java
* Copyright (C)  2003
* 
* $Id: Suake2.java,v 1.9 2005/12/03 19:43:15 salomo Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
using System;
using System.Globalization;
using System.Threading;
using Suake2.UI.qcommon;
using Timer=Suake2.UI.sys.Timer;

namespace Suake2.UI
{
    /// <summary> Suake2 is the main class of Quake2.NET.</summary>
    public sealed class Suake2
    {
        public static Q2DataDialog Q2Dialog;

        /// <summary> main is used to start the game. Quake2.NET supports the following
        /// command line arguments:
        /// 
        /// </summary>
        /// <param name="args">
        /// </param>
        [STAThread]
        public static void Main(String[] args)
        {
            bool dedicated = false;

            // check if we are in dedicated mode to hide the java dialog.
            for (int n = 0; n < args.Length; n++)
            {
                if (args[n].Equals("+set"))
                {
                    if (n++ >= args.Length)
                        break;

                    if (!args[n].Equals("dedicated"))
                        continue;

                    if (n++ >= args.Length)
                        break;

                    if (args[n].Equals("1") || args[n].Equals("\"1\""))
                    {
                        Com.Printf("Starting in dedicated mode.\n");
                        dedicated = true;
                    }
                }
            }

            // TODO: check if dedicated is set in config file

            Globals.dedicated = Cvar.Get("dedicated", "0", Defines.CVAR_NOSET);

            if (dedicated)
                Globals.dedicated.value_Renamed = 1.0f;


            // open the q2dialog, if we are not in dedicated mode.
            if (Globals.dedicated.value_Renamed != 1.0f)
            {
                Q2Dialog = new Q2DataDialog();
                //UPGRADE_TODO: Method 'java.util.Locale.setDefault' was converted to 'System.Threading.Thread.CurrentThread.CurrentCulture' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilLocalesetDefault_javautilLocale'"
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                //UPGRADE_TODO: Method 'java.awt.Component.setVisible' was converted to 'System.Windows.Forms.Control.Visible' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaawtComponentsetVisible_boolean'"
                //UPGRADE_TODO: 'System.Windows.Forms.Application.Run' must be called to start a main form. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1135'"
                Q2Dialog.Visible = true;
            }

            // in C the first arg is the filename
            int argc = (args == null) ? 1 : args.Length + 1;
            var c_args = new string[argc];
            c_args[0] = "Suake2";
            if (argc > 1)
            {
                Array.Copy(args, 0, c_args, 1, argc - 1);
            }
            Qcommon.Init(c_args);

            Globals.nostdout = Cvar.Get("nostdout", "0", 0);

            int oldtime = Timer.Milliseconds();
            int newtime;
            int time;
            while (true)
            {
                // find time spending rendering last frame
                newtime = Timer.Milliseconds();
                time = newtime - oldtime;

                if (time > 0)
                    Qcommon.Frame(time);
                oldtime = newtime;
            }
        }
    }
}